import React,{Component} from 'react';
import {connect} from 'react-redux';
import {AddCar} from "./../actions/counter";

import "./../Sass/spcar.scss";



class Spcar extends Component{

    reloadArr(){
        console.log(this.props.data_Car);
        var dataCarfnh=[];
        var ind = 0;
        for(var i=0;i<this.props.data_Car.length;i++){
            var flag = false;
            for(var j=0;j<dataCarfnh.length;j++){
                if(this.props.data_Car[i]._id==dataCarfnh[j]){
                    ind = j;
                    flag=true;
                }

            }
            if(flag){
                //已经有了



            }else{
                dataCarfnh.push(this.props.data_Car[i])
            }

        }
    }

    render(){
        if(this.props.data_Car.length>0){
            //购物车里有数据
            this.reloadArr();
            var myCar = this.props.data_Car.map(function(ele,index){
                const picUrl = "http://localhost:3000/"+ele.picture;
                return (
                    <div className="pieceGoods">
                        
                        <div className="row">
                            <div className="col-lg-4 picBox">
                                <img src={picUrl}/>
                            </div>
                            <div className="col-lg-8">
                                <h4>{ele.title}</h4>
                                <p>{ele.content}</p>
                                <p>我要买 <span>1</span> 件</p>
                            </div>

                        </div>
                    </div>
                )
            })


        }else{
            //购物车为空
            var myCar = (
                <div>
                    <h3>购物车里空空如也</h3>
                </div>
            )
        }

        return (
            <div id="spcar">
                <div className="container">
                    {myCar}
                </div>
            </div>
        )
    }
}

const getValue = (state)=>{
    return {
        // data_ori:state.dataGoodsOri,
        // data_goods:state.dataGoods,
        // data_cate:dataCate,
        data_Car:state.dataCar
    }
}

const spCarContent = connect(
    getValue,
    {AddCar}
)(Spcar)

module.exports = spCarContent;